﻿using CNKI.TPI.Web.Admin.IDAO;
using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Admin.DAO
{
    public class HomeConfigDAO : BaseDAO, IHomeConfigDAO
    {
        /// <summary>
        /// 根据类型获取首页配置信息
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig(string showtype)
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = new List<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG> exp = new ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
                exp.And("ShowType", OperatorType.Equal, showtype);
                result = Context.Get<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(exp.Compile());
                if (result != null)
                {
                    result = result.OrderBy(c => c.SortNo).ToList();
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 根据数据库类型ID，获取数据库类型
        /// </summary>
        /// <param name="databaseId"></param>
        /// <returns></returns>
        public SYS_CMS_RESOURCE_TEMPLATE GetTemplateByID(int datatypeId)
        {
            SYS_CMS_RESOURCE_TEMPLATE result = null;
            try
            {
                ExpressionCombine<SYS_CMS_RESOURCE_TEMPLATE> exp = new ExpressionCombine<SYS_CMS_RESOURCE_TEMPLATE>();
                exp.And("SYSID", OperatorType.Equal, datatypeId);
                IList<SYS_CMS_RESOURCE_TEMPLATE> content = Context.Get<SYS_CMS_RESOURCE_TEMPLATE>(exp.Compile());
                if (content != null && content.Count > 0)
                {
                    result = content[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
        /// <summary>
        /// 保存设置的栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveContentConfig(SYS_CMS_NEWS_NEWSCONTENTCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 修改设置的栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdateContentConfig(SYS_CMS_NEWS_NEWSCONTENTCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 保存设置的跳转栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveContentJump(SYS_CMS_NEWS_JUMPTEMPLATE obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_NEWS_JUMPTEMPLATE>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 修改设置的跳转栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdateContentJump(SYS_CMS_NEWS_JUMPTEMPLATE obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_NEWS_JUMPTEMPLATE>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 判断数据库是否已经设置过该栏目
        /// </summary>
        /// <param name="dbid"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public bool IsExistContentByID(int dbid,int typeid)
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = null;
            bool m = false;
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG> exp = new ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
                exp.And("DBID", OperatorType.Equal, dbid);
                exp.And("ShowType", OperatorType.Equal, typeid);
                result = Context.Get<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(exp.Compile());
                if (result != null && result.Count > 0)//该库已经设置过该栏目
                {
                    m = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return m;
        }
        /// <summary>
        /// 修改时，判断排除记录本身
        /// </summary>
        /// <param name="dbid"></param>
        /// <param name="typeid"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistContentByID(int dbid,int typeid, int sysid)
        {
            bool result = false;
            string sql = "SELECT * FROM SYS_CMS_NEWS_NEWSCONTENTCONFIG where dbid=" + dbid + " and showtype=" + typeid;
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> obj = null;
            try
            {
                obj = Context.ExecuteSql<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(sql);
                if (obj != null && obj.Count > 0 && obj[0].SysID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
        /// <summary>
        /// 判断数据库是否已经设置过跳转模板
        /// </summary>
        /// <param name="dbid"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public bool IsExistJumpByID(int dbid,int showtype)
        {
            IList<SYS_CMS_NEWS_JUMPTEMPLATE> result = null;
            bool m = false;
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_JUMPTEMPLATE> exp = new ExpressionCombine<SYS_CMS_NEWS_JUMPTEMPLATE>();
                exp.And("DBID", OperatorType.Equal, dbid);
                exp.And("TemplateType", OperatorType.Equal, showtype);
                result = Context.Get<SYS_CMS_NEWS_JUMPTEMPLATE>(exp.Compile());
                if (result != null && result.Count > 0)//该库已经设置过该栏目
                {
                    m = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return m;
        }

        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfigByID(int sysid)
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = new List<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG> exp = new ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
                exp.And("SysID", OperatorType.Equal, sysid);
                result = Context.Get<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public IList<SYS_CMS_NEWS_JUMPTEMPLATE> GetHomepageJumpByID(int sysid)
        {
            IList<SYS_CMS_NEWS_JUMPTEMPLATE> result = new List<SYS_CMS_NEWS_JUMPTEMPLATE>();
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_JUMPTEMPLATE> exp = new ExpressionCombine<SYS_CMS_NEWS_JUMPTEMPLATE>();
                exp.And("SysID", OperatorType.Equal, sysid);
                result = Context.Get<SYS_CMS_NEWS_JUMPTEMPLATE>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool DeleteContentNew(SYS_CMS_NEWS_NEWSCONTENTCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Delete(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
        public bool DeleteJump(SYS_CMS_NEWS_JUMPTEMPLATE obj)
        {
            bool result = false;
            try
            {
                result = Context.Delete(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }


        /// <summary>
        /// 修改时，判断排除记录本身
        /// </summary>
        /// <param name="dbid"></param>
        /// <param name="typeid"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistJumpByID(int dbid, int typeid, int sysid)
        {
            bool result = false;
            string sql = "SELECT * FROM SYS_CMS_NEWS_JUMPTEMPLATE where dbid=" + dbid + " and TemplateType=" + typeid;
            IList<SYS_CMS_NEWS_JUMPTEMPLATE> obj = null;
            try
            {
                obj = Context.ExecuteSql<SYS_CMS_NEWS_JUMPTEMPLATE>(sql);
                if (obj != null && obj.Count > 0 && obj[0].SysID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_MENUCONFIG> GetMenuList(ExpressionCombine<SYS_CMS_NEWS_MENUCONFIG> exp, int cacheTime)
        {
            IList<SYS_CMS_NEWS_MENUCONFIG> result = new List<SYS_CMS_NEWS_MENUCONFIG>();
            try
            {
                Expression<Func<SYS_CMS_NEWS_MENUCONFIG, bool>> order = c => c.Sort.Asc();
                if (exp != null)
                {
                    result = Context.Get<SYS_CMS_NEWS_MENUCONFIG>(exp.Compile(),order:order, dataCacheTimeout:cacheTime);
                }
                else
                {
                    result = Context.GetAll<SYS_CMS_NEWS_MENUCONFIG>(order: order, dataCacheTimeout: cacheTime);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public bool AddMenu(SYS_CMS_NEWS_MENUCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_NEWS_MENUCONFIG>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public bool UpdateMenu(SYS_CMS_NEWS_MENUCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_NEWS_MENUCONFIG>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public bool DelMenu(SYS_CMS_NEWS_MENUCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Delete<SYS_CMS_NEWS_MENUCONFIG>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 修改菜单显示顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateMenuSort(IList<SYS_CMS_NEWS_MENUCONFIG> list)
        {
            bool m = true;
            try
            {
                string sql = "";
                foreach (SYS_CMS_NEWS_MENUCONFIG item in list)
                {
                    sql = " update SYS_CMS_NEWS_MENUCONFIG set Sort='" + item.Sort + "' where SYSID='" + item.SYSID + "'";
                    Context.ExecuteNonQuery(sql);
                }
            }
            catch
            {
                m = false;
            }
            return m;

        }
        /// <summary>
        /// 更改首页栏目显示顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateHomeSort(IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list)
        {
            bool m = true;
            try
            {
                string sql = "";
                foreach (SYS_CMS_NEWS_NEWSCONTENTCONFIG item in list)
                {
                    sql = " update SYS_CMS_NEWS_NEWSCONTENTCONFIG set SortNo='" + item.SortNo + "' where SYSID='" + item.SysID + "'";
                    Context.ExecuteNonQuery(sql);
                }
            }
            catch
            {
                m = false;
            }
            return m;

        }
    }
}
